package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.entity.TravelListContent;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.text.ParseException;
import java.util.HashMap;
import org.json.JSONException;

public class EditManageReimburseListDetailFragment extends ManageReimburseListDetailFragment {
  public static final String EXTRA_TRAVELLIST_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_TRAVELLIST_CONTENT";

  private static final int REQUEST_DELETE = 0;
  private static final int REQUEST_ALTER = 1;// 修改
  private TravelListContent mTravelListContent;

  // 创建菜单
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);

    mTravelListContent =
        (TravelListContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BaseVolleyActivity) getActivity();
    mTravelListContent =
        (TravelListContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
  }

  // 初始化控件
  protected void initData() {
    // 详情
    String date = mTravelListContent.getFEE_DATE();
    if (!TextUtils.isEmpty(date)) {
      try {
        date = DateUtil.dateToString(date, DateUtil.TYPE_DATE);
      } catch (ParseException e) {

        e.printStackTrace();
      }
    }
    tvappendDate.setText(date);
    tvfareType.setText(mTravelListContent.getFEE_KIND());

    etfeeAmount.setText(mTravelListContent.getFEE_AMOUNT());
    etbillNum.setText(mTravelListContent.getBILL_NUM());
    etbillNo.setText(mTravelListContent.getBILL_NO());
    etfeeNote.setText(mTravelListContent.getFEE_NODE());
    etnote.setText(mTravelListContent.getNOTE());
    tvInvestigateContent.setText(mTravelListContent.getINVESTIGATE_CONTENT());

    mfeeKindId = mTravelListContent.getFEE_KINDID();
    mfeeKindName = mTravelListContent.getFEE_KIND();
    mfeeKindLen = mTravelListContent.getREIMBURSEMENT_WAY();
    mfeeKindFlag = mTravelListContent.getADDITION_ITEM();
  }

  // 菜单点击事件
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_confirm:

        boolean allEmpty = allEmpty();

        if (allEmpty) {
          Toast.makeText(getActivity(), R.string.nothing, Toast.LENGTH_SHORT).show();
        } else {
          volleyPostSave();
        }

        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void volleyPostSave() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_TRAVEL_LIST_SAVE);

    HashMap<String, String> params = new HashMap<>();
    //		// 更新content

    mTravelListContent.setFEE_DATE(tvappendDate.getText().toString());

    // 费用分类
    if (!TextUtils.isEmpty(mfeeKindId)) {
      mTravelListContent.setFEE_KIND(tvfareType.getText().toString());//费用分类
      mTravelListContent.setFEE_KINDID(mfeeKindId);//费用分类id
      mTravelListContent.setREIMBURSEMENT_WAY(mfeeKindLen);//费用分类len
      mTravelListContent.setADDITION_ITEM(mfeeKindFlag);//费用分类flag
    }
    //		paramsItem.put("FEE_AMOUNT",etfeeAmount.getText().toString());
    //		paramsItem.put("BILL_NUM",etbillNum.getText().toString());
    //		paramsItem.put("BILL_NO",etbillNo.getText().toString());
    //		paramsItem.put("FEE_NODE",etfeeNote.getText().toString());
    //		paramsItem.put("NOTE",etnote.getText().toString());
    mTravelListContent.setFEE_AMOUNT(etfeeAmount.getText().toString());
    mTravelListContent.setBILL_NUM(etbillNum.getText().toString());
    mTravelListContent.setBILL_NO(etbillNo.getText().toString());
    mTravelListContent.setFEE_NODE(etfeeNote.getText().toString());
    mTravelListContent.setNOTE(etnote.getText().toString());

    params.put("json", new Gson().toJson(mTravelListContent));
    LogUtil.i(new Gson().toJson(mTravelListContent));
    params.put("regJobNo", mCurrentUser.getJobNumber());//申请人工号
    params.put("mainId", mTravelListContent.getMAINID());//主表id
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        SuccessMessage[] result = new Gson().fromJson(arg0, SuccessMessage[].class);
        SuccessMessage workProcess = result[0];
        if (null == workProcess || null == workProcess.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static EditManageReimburseListDetailFragment newInstance(
      TravelListContent mTravelListContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_TRAVELLIST_CONTENT, mTravelListContent);
    EditManageReimburseListDetailFragment fragment = new EditManageReimburseListDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
